package com.shushan.home.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: AbsCommonAdapter
 * @Desciption: 万能通用适配器
 * @author: yichaohua
 * @date: 2018-01-05
 */
public abstract class AbsCommonAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;
    protected int selectedItem = -1;

    public AbsCommonAdapter(Context context, int itemLayoutId, List<T> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
    }

    public AbsCommonAdapter(Context context, int itemLayoutId, T[] mDatas) {
        this.mContext = context;
        this.mDatas = Arrays.asList(mDatas);
        this.mItemLayoutId = itemLayoutId;
    }

    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = getViewHolder(position, convertView, parent);
        viewHolder.setPosition(position);
        convert(viewHolder, getItem(position), selectedItem);
        return viewHolder.getConvertView();
    }

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
    }

    public abstract void convert(ViewHolder helper, final T item, final int _selectedItem);

    public void add(List<T> list) {
        if (list == null || list.size() == 0)
            return;
        this.mDatas.addAll(list);


    }

    public void replace(List<T> list) {
        if (list == null || list.size() == 0)
            return;
        this.mDatas.clear();
        this.mDatas.addAll(list);

    }

    public void add(T t) {
        this.mDatas.add(t);
    }

    public void add(int position, T t) {
        this.mDatas.add(position, t);
    }

    public void clearAll() {
        this.mDatas.clear();
    }

    public void clearAll2() {
        this.mDatas = new ArrayList<>();
        notifyDataSetChanged();
    }

    public void removeItem(T t) {
        this.mDatas.remove(t);
    }

    public void removeItem(int position) {
        this.mDatas.remove(position);
    }

    public List<T> getData() {
        return mDatas;
    }

    public void setSelectedItem(int selectedItem) {
        this.selectedItem = selectedItem;
    }
    public  int getSelectedItem(){return this.selectedItem;}
    /**
     * 公共ViewHolder对象
     *
     * @author yinbiao
     *
     * @date 2015-6-9下午2:16:04
     */
    public static class ViewHolder {
        private final SparseArray<View> mViews;
        private View mConvertView;
        private int position;

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
            this.mViews = new SparseArray<View>();
            mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            mConvertView.setTag(this);
        }

        /**
         * 拿到一个ViewHolder对象
         *
         * @param context
         * @param convertView
         * @param parent
         * @param layoutId
         * @param position
         * @return
         */
        public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
            if (convertView == null) {
                return new ViewHolder(context, parent, layoutId, position);
            }
            return (ViewHolder) convertView.getTag();
        }

        public View getConvertView() {
            return mConvertView;
        }

        /**
         * 通过控件的Id获取对于的控件，如果没有则加入views
         *
         * @param viewId
         * @return
         */
        @SuppressWarnings("unchecked")
        public <T extends View> T getView(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = mConvertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }

        /**
         * 为TextView设置字符串
         *
         * @param viewId
         * @param text
         * @return
         */
        public void setText(int viewId, String text) {
            TextView view = getView(viewId);
            view.setText(text);
        }

        /**
         * 为ImageView设置图片
         *
         * @param viewId
         * @param drawableId
         * @return
         */
        public void setImageResource(int viewId, int drawableId) {
            ImageView view = getView(viewId);
            view.setImageResource(drawableId);
        }

        /**
         * 为ImageView设置图片
         *
         * @param viewId
         * @param bm
         * @return
         */
        public void setImageBitmap(int viewId, Bitmap bm) {
            ImageView view = getView(viewId);
            view.setImageBitmap(bm);
        }
    }
}
